﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Mbs;
using Mbs.Messaging;

namespace MBTest
{
    [TestFixture]
    public class PersonTagFilterTest
    {
        static MessageBus Actor;

        [SetUp]
        public virtual void Setup()
        {
            Actor = new MessageBus();
        }


        class Person : IPersonMessageHandler
        {
            _ Topics;

            class _
            {
               public _(string name)
               {
                   Chat = name + ".Chat";
                   Eat = name + ".Eat";
                   Work = name + ".Work";
               }

                public string Chat { get;private set;}
                public string Eat { get;private set;}
                public string Work { get;private set;}
            }

            public string Name { get; private  set; }


            public Person(string name)
            {
                Name = name;
                Topics = new _(name);
                Actor.Subscribe<ChatMessage, EatMessage, WorkMessage>(
                    Topics.Chat, (s, e) =>
                    {
                        var sender = s as Person;
                        Console.WriteLine(string.Format("[{0}] 正在和[{1}] 谈论[{2}].", sender.Name, e.Message.Another.Name, e.Message.Topic));

                        const string exitContent = "烦不烦，天天讨论天气,拜拜！！！！";
                        if (e.Message.Topic.Content == exitContent)
                            Actor.Remove<ChatMessage>(Topics.Chat);
                        else
                            Chat(sender, new Topic { Name = e.Message.Topic.Name, Content = exitContent });
                    },
                    Topics.Eat, (s, e) =>
                    {
                        Console.WriteLine(string.Format("[{0}] 正在[{1}] 的 酒店就餐.", Name, e.Message.Restaurant.Address));
                    },
                    Topics.Work, (s, e) =>
                    {
                        Console.WriteLine(string.Format("[{0}] 正在为他的上级[{1}] 做[{2}].", Name, e.Message.ReportTo.Name, e.Message.Job.Name));
                    }
                    );
            }

            public void Chat(Person another, Topic topic)
            {
                Actor.Publish<ChatMessage>(another.Name + ".Chat", this, new ChatMessage { Topic = topic, Another = another });
            }

            public void Eat(Restaurant restaurant)
            {
                Actor.Publish<EatMessage>(Topics.Eat, this, new EatMessage { Restaurant = restaurant });
            }

            public void Work(Person reportTo, Job job)
            {
                Actor.Publish<WorkMessage>(Topics.Work, this, new WorkMessage { Job = job, ReportTo = reportTo });
            }

            public override string ToString()
            {
                return Name;
            }
        }

        class Topic
        {
            public string Name;
            public string Content;
            public override string ToString()
            {
                return Name + " Content:" + Content;
            }
        }

        class ChatMessage
        {
            public Person Another;
            public Topic Topic;
        }

        class Restaurant { public string Address; }

        class EatMessage
        {
            public Restaurant Restaurant;
        }

        class Job
        {
            public string Name;
        }

        class WorkMessage
        {
            public Job Job;
            public Person ReportTo;
        }

        interface IPersonMessageHandler
        {
            void Chat(Person another, Topic topic);
            void Eat(Restaurant restaurant);
            void Work(Person reportTo, Job job);
        }

        [Test]
        public void Test()
        {
            var zhangSan = new Person ("张三" );
            var liSi = new Person("李四" );
            var wangWu = new Person("王五" );

            zhangSan.Chat(liSi, new Topic { Name = "天气", Content = "今天天气也太热了，真受不了。。。。" });
            wangWu.Eat(new Restaurant { Address = "王府井大街" });

            liSi.Work(wangWu, new Job { Name = "客服" });
        }


    }
}
